
const app = getApp()
const md5 = require('../../../utils/md5.js');
const http = require('../../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      latitude: '',
      longitude: '',
        markers: [],
        polyline:[],
        mapControls: [{   //地图中心
            id: '0',
            position: {                 //相对定位
                left: app.globalData.scWidth * 0.47,
                top: app.globalData.scHeight * 0.42,
                width: app.globalData.scHeight * 0.04,
                height:app.globalData.scWidth * 0.09
            },
            iconPath: "/images/d0.png",
            clickable: false
        }],
        hidden:'none',
        show:'',
       identify:false,
       qqhidden:false,
       realhidden:false,
       canIUse:false,
       codeRust:'',
       orderId:'',
       messagePrompt:false
      
       
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        /** 这是检测是否还有未完成的订单 **/
        var _this = this;
      _this.computers()
      wx.getSetting({       
          success: function (res) {
              console.log(res)
              if (!res.authSetting['scope.userInfo']) {
                 _this.setData({
                     canIUse:true
                 })
              }else{
                 
                   console.log(wx.getStorageSync('token'))
                    if (!wx.getStorageSync('token')) {
                      wx.getUserInfo({
                          success: function (resMsg) {
                             console.log(resMsg)
                              wx.login({                                  
                                  success: res => {
                                      console.log(res.code)
                                      var that = this;
                                      var data = {
                                          method: 'Login.auth',
                                          code: res.code,
                                          encryptedData: resMsg.encryptedData,
                                          iv: resMsg.iv,
                                      }
                                      var arr = http.sorts(data)
                                      data.sign = md5.hex_md5(arr)
                                      //插入登录的用户的相关信息到数据库
                                      http._post(data, res => {
                                          console.log(res)
                                          if (res.data.code == 0) {
                                              wx.setStorageSync('token', res.data.data.token)
                                              _this.computers()
                                              console.log("插入小程序登录用户信息成功！");
                                              if (res.data.data.isBinding == '0') {
                                                  wx.redirectTo({
                                                      url: '/pages/index/index',
                                                  })
                                              }
                                          } else { console.log(res) }
                                      }, err => { })
                                  }
                              })
                          }
                      })
                   }
              }
          }
      })
  

  },
  //获取有多少电脑
  computers(){
      let _this = this;
      wx.getLocation({
          type: 'gcj02',
          success: (res) => {
              //获取附近电脑的接口
            //   let msgList = {
            //       method: 'Scan.code',
            //       token: wx.getStorageSync('token'),
            //       latitude: res.latitude,
            //       longitude: res.longitude
            //   }
            //   let arrs = http.sorts(msgList);
            //   msgList.sign = md5.hex_md5(arrs);
            //   http._post(msgList, res => {
            //       console.log(res)
            //       if (res.data.code == 0) {
            //           console.log(res)
            //       }
            //   })
              _this.setData({
                  latitude: res.latitude,
                  longitude: res.longitude,
                  markers: [
                      //{
                      //       id: "0",
                      //       latitude: res.latitude,
                      //       longitude: res.longitude,
                      //       iconPath: '/images/d2.png',
                      //       width: 30,
                      //       height: 32
                      //   }, 
                      {
                          id: "1",
                          latitude: res.latitude - 0.001,
                          longitude: res.longitude,
                          iconPath: '/images/d1.png',
                          width: 30,
                          height: 32,
                          callout: {
                              content: "地址",
                              bgColor: "#ffffff",
                              color: "#ff0000",
                              padding: 10,
                              display: "ALWAYS",
                              borderRadius: 5,                             
                          }
                      }, {
                          id: "2",
                          latitude: res.latitude + 0.0007,
                          longitude: res.longitude,
                          iconPath: '/images/d1.png',
                          width: 30,
                          height: 32,
                          callout: {
                              content: "地址",
                              bgColor: "#ffffff",
                              color: "#ff0000",
                              padding: 10,
                              display: "ALWAYS",
                              borderRadius: 5,                             
                          }
                      }, {
                          id: "3",
                          latitude: res.latitude,
                          longitude: res.longitude - 0.001,
                          iconPath: '/images/d1.png',
                          width: 30,
                          height: 32,
                          callout: {
                              content: "地址",
                              bgColor: "#ffffff",
                              color: "#ff0000",
                              padding: 10,
                              display: "ALWAYS",
                              borderRadius: 5,
                          }
                      }

                  ],

              })

          }
      })
  },
    code(e) {
        var _this = this
        wx.login({
            success: res => {
                var that = this;
                var data = {
                    method: 'Login.auth',
                    code: res.code,
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                }
                var arr = http.sorts(data)
                data.sign = md5.hex_md5(arr)
                console.log(data)
                //插入登录的用户的相关信息到数据库
                http._post(data, res=>{
                    console.log(res)
                    if (res.data.code == 0) {
                            wx.setStorageSync('token', res.data.data.token)
                            _this.setData({
                                canIUse: false
                            })
                            _this.computers()
                            console.log("插入小程序登录用户信息成功！");
                            if (res.data.data.isBinding=='0') {                        
                                wx.redirectTo({
                                    url: '/pages/index/index',
                                })
                            }
                    }else{console.log(res)}
                },err=>{})
            }
        })
    },
  regionchange(e){ //视图移动重新获取地图中心点
     
    /** 中心点的位置 **/
      if (e.type == "end") {
          var that = this;
          console.log(that);
          this.mapCtx.getCenterLocation({
            success:(res)=>{
                console.log(res.latitude);
                console.log(res.longitude);
            }
          })
      }
  },
   markerlout(e) {
       
      console.log(e.markerId)        
   },
   markertap(e){
       console.log(e)
      // console.log(e.markerId) 
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
      this.mapCtx = wx.createMapContext('map');
      this.mapCtx.moveToLocation()
  },

  includePoints: function () {
      this.mapCtx.includePoints({
          padding: [10],
          points: [{
              latitude: this.data.latitude,
              longitude: this.data.longitude,
          }]
      })
      
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     let _this = this
    if (wx.getStorageSync('token')){
        let data={
            method:'User.isOrder',
            token:wx.getStorageSync('token')
        }
        let arr=http.sorts(data)
        data.sign = md5.hex_md5(arr)
        http._post(data,res=>{
            console.log(res)
            if (res.data.code == 0 && res.data.data.orderId!=0){
                _this.setData({
                    orderId: res.data.data.orderId
                })          
            }        
            res.data.data.isOrder == 1 ? _this.setData({ messagePrompt: true }) : _this.setData({ messagePrompt: false })         
        })
    }
  },
  clickInfo(){
      wx.navigateTo({
          url: '/pages/myinfo/myinfo',
      })
  },
  clickInfomation() {
    wx.navigateTo({
      url: '/pages/myinfo/Infomation/Infomation',
    })        
  },
  clickCode(){
      let that=this;
      let show;
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {  
                console.log(res)  
                           
            let data={
                 method:'Scan.code',
                 token:wx.getStorageSync('token'),
                 codeInfo:res.result
             }   
             let arr = http.sorts(data)
                data.sign = md5.hex_md5(arr)
                http._post(data,res=>{                   
                    console.log(res)
                    
                    if(res.data.code=='80000'){
                        //实名认证
                        that.setData({
                            realhidden:true                           
                        })
                    } else if (res.data.code =='80200'){
                        //余额不足
                        that.setData({
                            qqhidden: true
                        })
                    } else if (res.data.code =='80300'){
                        console.log('sdf:' +res.data.data.orderId)
                        //还在上机
                        that.setData({
                            hidden: 'block',
                            orderId: res.data.data.orderId
                        })

                    }else if(res.data.code=='0'){
                        console.log('扫码：' + JSON.stringify(res))        
                        //扫码成功
                        that.setData({
                            codeRust: JSON.stringify(res.data.data.coding).replace(/\"/g, ""),
                            identify: true
                        })
                        setTimeout(function () { that.takePhoto() }, 3000)
                    }else{            
                        wx.showToast({
                            title:res.data.msg,
                            icon:'none',
                            duration:2000
                        })
                    }
                })
           
                
            // wx.showToast({
            //   title: '成功',
            //   icon: 'success',
            //   duration: 2000
            // })                        
           
          },
          fail: (res) => {
            wx.showToast({
              title: '失败',
              icon: 'none',
              duration: 2000
            })
          },
          complete: (res) => {
          }              
        })
    },
    //人脸识别
    takePhoto() {
        let _this = this;
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
               console.log(res.tempImagePath);
                this.setData({
                    // src: res.tempImagePath,
                })
                // console.log(src),
                wx.showLoading({
                    title: '正在核验身份',
                })
                let msg={
                    method:'File.uploadImg',
                    token:wx.getStorageSync('token'),
                    type:4
                } 
                let arr = http.sorts(msg)
                msg.sign = md5.hex_md5(arr)
                wx.uploadFile({
                    url: http.http, //仅为示例，非真实的接口地址
                    filePath: res.tempImagePath,
                    name: 'file',
                    formData:msg,
                    success: (res) => {
                        console.log(res)
                        let faceData = {
                            method: 'Auth.faceRecognition',
                            token:wx.getStorageSync('token'),
                            contrastFace: JSON.parse(res.data).data.url,
                            coding: _this.data.codeRust,
                            latitude: _this.data.latitude,
                            longitude: _this.data.longitude                            
                        }
                        let faceArr=http.sorts(faceData)
                        faceData.sign = md5.hex_md5(faceArr)
                        http._post(faceData,res=>{
                            console.log('成功id:' + JSON.stringify(res.data.data.orderId).replace(/\"/g, ""))
                            if(res.data.code==0){
                                _this.setData({                                   
                                    messagePrompt: true
                                })  
                            }
                            _this.setData({                               
                                orderId: JSON.stringify(res.data.data.orderId).replace(/\"/g,"") 
                            })
                            _this.setData({
                                identify: false,
                            })  
                             wx.hideLoading();      
                             wx.showToast({
                                 title: res.data.msg,
                                 icon:'none',
                                 duration:2000
                             })
                        })                                                                                                            
                    }
                })
            }
        })
    },
    close(){
        this.setData({
            hidden:'none'
        })
    },
    look(){
        wx.navigateTo({
            url: '/pages/myinfo/orderList/orderTheTough/orderTheTough?orderId=' + this.data.orderId,
        })
    },
    // 关闭弹窗
    qq_close(val){
        console.log(val)
        if (val.currentTarget.dataset.select==2){   
            this.setData({
                realhidden:false
            })
        } else { 
            this.setData({
                qqhidden: false
            })
        }
    },
    //钱不够确认跳转
    qq_yes(){
        wx.navigateTo({
            url: '/pages/myinfo/myMoney/recharge/recharge',
        })
    },
    real_yes(){
        wx.navigateTo({
            url: '/pages/index/realMsg/realMsg',
        })
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})